import 'package:flutter/material.dart';
import 'package:provide/provide.dart';
import 'package:xv_flutter/pages/about_us_page.dart';
import 'package:xv_flutter/pages/home_page.dart';
import 'package:xv_flutter/pages/news_page.dart';
import 'package:xv_flutter/pages/product_page.dart';
import 'package:xv_flutter/provide/current_index.dart';

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  // 切换页面
  final tabBars = [
    HomePage(),
    ProductPage(),
    NewsPage(),
    AboutUsPage(),
  ];

  // 底部导航按钮
  final bottomTabs = [
    BottomNavigationBarItem(title: Text('首页'), icon: Icon(Icons.home)),
    BottomNavigationBarItem(title: Text('产品'), icon: Icon(Icons.apps)),
    BottomNavigationBarItem(title: Text('新闻'), icon: Icon(Icons.fiber_new)),
    BottomNavigationBarItem(title: Text('关于我们'), icon: Icon(Icons.insert_comment)),
  ];

  @override
  Widget build(BuildContext context) {
    return Provide<CurrentIndexProvide>(
      builder: (context, child, val) {
        int currentIndex = Provide.value<CurrentIndexProvide>(context).currentIndex;
        return Scaffold(
          appBar: AppBar(
            title: Text('玫瑰花海'),
            leading: Icon(Icons.home),
            actions: <Widget>[
              // 右侧内边距20
              Padding(
                padding: EdgeInsets.only(right: 20),
                child: GestureDetector(
                  onTap: () {},
                  child: Icon(Icons.search),
                ),
              )
            ],
          ),
          body: IndexedStack(
            index: currentIndex,
            children: tabBars,
          ),
          bottomNavigationBar: BottomNavigationBar(
            type: BottomNavigationBarType.fixed,
            currentIndex: currentIndex,
            onTap: (index) {
              Provide.value<CurrentIndexProvide>(context).changeIndex(index);
            },
            items: bottomTabs,
          ),
        );
      },
    );
  }
}
